package cn.tedu._05mvcboot02.exception;

import cn.tedu._05mvcboot02.common.response.JsonResult;
import cn.tedu._05mvcboot02.common.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleIllegalArgumentException(IllegalArgumentException ex){
        String message = ex.getMessage();
        log.error("异常消息:"+message);
        return new JsonResult(StatusCode.OPERATION_FAILED,message);
    }

    @ExceptionHandler
    public JsonResult doHandleRuntimeException(RuntimeException ex){
        String message = ex.getMessage();
        log.error("异常消息:"+message);
        return new JsonResult(StatusCode.OPERATION_FAILED,message);
    }

    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        String message = ex.getMessage();
        log.error("异常处理:"+message);
        return new JsonResult(StatusCode.VALIDATED_ERROR,message);
    }

    @ExceptionHandler
    public JsonResult doHandleThrowableException(Exception ex){
        String message = ex.getMessage();
        log.error("异常处理:"+message);
        return new JsonResult(StatusCode.THROWABLE_ERROR,message);
    }
}
